<?php
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Db;
use think\Model;
class Favorite extends Model{
	protected $insert = ['favorite_addtime','favorite_cid'];
	protected function setFavoriteCidAttr($value){
		if($value){
			$pid=getlistname($value,'list_pid');
		    return $pid == 0 ? $value : $pid;
	    }
	}		
	protected function setFavoriteAddtimeAttr($value,$data){
		return time();
	}
	public function user(){
		return $this->hasOne('user','user_id','favorite_uid');
    }
	public function vod(){
		return $this->hasOne('vod','vod_id','favorite_vid');
    }	
 	public function isFavoriteVod($where){
		$count = $this->where($where)->count();
		return $count > 0 ? true : false ;
	} 
	public function getFavoriteCatalogs($userid){
		$list = $this->field('*,count(list_id) as count')->alias('r')->join('list b','b.list_id=r.favorite_cid')->where('favorite_uid',$userid)->group('list_id,list_name')->select();
		return $list;
	}	
	public function getFavoriteDatas($userid){
		//print_r($userid) ;
        $list = Db::query("SELECT vod_id,vod_name,vod_letters,vod_cid,vod_pic,vod_title,vod_addtime,vod_continu,vod_gold,vod_actor,vod_year,vod_jumpurl FROM ".config('database.prefix')."vod  inner join ( select b.favorite_vid,count(b.favorite_vid) as fnum from ".config('database.prefix')."favorite b where not exists(select favorite_vid from ".config('database.prefix')."favorite c where favorite_uid = ".$userid." and b.favorite_vid = c.favorite_vid ) group by b.favorite_vid ) a  on ".config('database.prefix')."vod.vod_id = a.favorite_vid  ORDER BY  a.fnum desc , ".config('database.prefix')."vod.vod_hits_day desc  LIMIT 10");
		if(empty($list)){
        $list = Db::query("SELECT vod_id,vod_name,vod_letters,vod_cid,vod_pic,vod_title,vod_addtime,vod_continu,vod_gold,vod_actor,vod_year,vod_jumpurl FROM ".config('database.prefix')."vod  WHERE  vod_id  NOT  IN  ( SELECT favorite_vid FROM ".config('database.prefix')."favorite) ORDER BY ".config('database.prefix')."vod.vod_hits desc  LIMIT 10");
        }
		return $list ;
	}
   //数据查询	
    public function listData($tag){
		$param = zanpiancms_param_url();
		if(!is_array($tag)) {
            $tag=json_decode($tag, true);
        }
		$tag['field']= !empty($tag['field']) ? $tag['field'] : 'vod_id,vod_name,vod_letters,vod_cid,vod_pic,vod_title,vod_addtime,vod_continu,vod_gold,vod_actor,vod_year,vod_addtime,vod_jumpurl,f.*';
		$tag['limit']= !empty($tag['limit']) ? $tag['limit'] : 10;
		if(!empty($tag['page']) && !empty($param['order'])){
			$tag['order']='vod_'.$param['order'].' desc';
		}		
		if(empty($tag['order'])){
			$tag['order']='vod_addtime desc';
		}	
		$where=[];
		$where['vod_status']=['eq',1];		
	    if(!empty($tag['ids'])){
		    $ids = explode(',',$tag['ids']);
		    if(count($ids)>1) {
			    $where['vod_id'] = array('in',$tag['ids']);
		    }else{
			    $where['vod_id'] = array('eq',$ids[0]);
		    }
	    }
	    if(!empty($tag['uid'])) {
		    $uids = explode(',',trim($tag['uid']));
		    if(count($uids) > 1) {
			    $where['favorite_uid'] = array('in',$tag['uid']);
		    }else{
			    $where['favorite_uid'] = array('eq',$uids[0]);
		    }
	    }
	    if(!empty($tag['cid'])){
		    $rcid = explode(',',$tag['cid']);
		    if(count($rcid)>1) {
			    $where['favorite_cid'] = array('in',$tag['cid']);
		    }else{
			    $where['favorite_cid'] = array('eq',$rcid[0]);
		    }
	    }		
        if(!empty($tag['addtime'])){
		    $time = explode(',',$tag['addtime']);
		    if(count($time) > 1) {
			    $where['favorite_addtime'] = array('between',intval(strtotime($time[0])).','.intval(strtotime($time[1])));
		    }else{
			    $where['favorite_addtime'] = array('gt',intval(strtotime($time[0])));
		    }			
        }	
        if(!empty($tag['page'])){
			$route=$GLOBALS['route'];
            $url=urldecode(url($route,config('params')));
		    $data = db('favorite')->alias('f')->join('vod b','b.vod_id=f.favorite_vid','LEFT')->where($where)->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
		}else{
			$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			$data = db('favorite')->alias('f')->join('vod b','b.vod_id=f.favorite_vid','LEFT')->where($where)->order($tag['order'])->limit($limit_str)->select();
		}	
		return $data;
    }		
	
}